package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateRobotAccountGetOneDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateRobotAccountGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateRobotAccountSaveDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateRobotAccountUpdateDTO;
import com.itlong.cloud.POJO.PO.AppRobotAccountPO;
import com.itlong.cloud.POJO.VO.operate.OperateRobotAccountGetOneVO;
import com.itlong.cloud.POJO.VO.operate.OperateRobotAccountGetPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateRobotAccountDao;
import com.itlong.cloud.operate.service.IOperateRobotAccountService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.encrypt.Md5;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     运营-机器人项目账号管理服务层接口实现类
 * <desc/>
 *
 * @createDate 2018/10/11
 */
@Service
public class OperateRobotAccountServiceImpl implements IOperateRobotAccountService {

    @Autowired
    private IOperateRobotAccountDao iOperateRobotAccountDao;

    /**
     * <desc>
     *      添加机器人账号
     * </desc>
     *
     * @param operateRobotAccountSaveDTO 添加机器人账号信息数据DTO
     * @return 受影响行数
     * @author wangzhi
     * @createDate 2018/10/12
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer saveRobotAccount(OperateRobotAccountSaveDTO operateRobotAccountSaveDTO) throws Exception {
        Date nowTime = new Date();
        Date operationTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(operateRobotAccountSaveDTO.getOperationTimeString());
        Date abateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(operateRobotAccountSaveDTO.getAbateTimeString());
        AppRobotAccountPO appRobotAccountPO = new AppRobotAccountPO();
        BeanUtils.copyProperties(operateRobotAccountSaveDTO,appRobotAccountPO);
        appRobotAccountPO.setPassword(Md5.En(appRobotAccountPO.getPassword()));
        appRobotAccountPO.setAppRobotAccountId(LogicIdUtil.bussinessId());
        appRobotAccountPO.setOperationTime(operationTime);
        appRobotAccountPO.setAbateTime(abateTime);
        appRobotAccountPO.setCreateTime(nowTime);
        appRobotAccountPO.setUpdateTime(nowTime);
        appRobotAccountPO.setStatus(DataStatusEnum.NORMAL.getType());
        appRobotAccountPO.setDeveloperUserId(operateRobotAccountSaveDTO.getUserId());
        return iOperateRobotAccountDao.saveRobotAccount(SqlUtil.durableData(appRobotAccountPO,PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      查询单个机器人账号信息
     * </desc>
     *
     * @param operateRobotAccountGetOneDTO 查询单个机器人账号信息数据DTO
     * @return 单个机器人账号信息
     * @author wangzhi
     * @createDate 2018/10/12
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateRobotAccountGetOneVO getOneRobotAccount(OperateRobotAccountGetOneDTO operateRobotAccountGetOneDTO) throws Exception{
        return iOperateRobotAccountDao.getOneRobotAccount(SqlUtil.convertBeanToMapParams(operateRobotAccountGetOneDTO));
    }

    /**
     * <desc>
     *      分页查询机器人账号信息
     * </desc>
     *
     * @param operateRobotAccountGetPageDTO 分页查询机器人账号信息数据DTO
     * @return 查询结果集
     * @author wangzhi
     * @createDate 2018/10/12
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateRobotAccountGetPageVO> getRobotAccountByPage(OperateRobotAccountGetPageDTO operateRobotAccountGetPageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(operateRobotAccountGetPageDTO);
        Page<OperateRobotAccountGetPageVO> page = new Page<>();
        page.getPage().setPageSize(operateRobotAccountGetPageDTO.getPageSize());
        page.getPage().setCurrentPage(operateRobotAccountGetPageDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<OperateRobotAccountGetPageVO> operateRobotAccountGetPageVOS = iOperateRobotAccountDao.getRobotAccountByPage(params);
        page.setRows(operateRobotAccountGetPageVOS,iOperateRobotAccountDao.getRobotAccountRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      修改机器人账号信息
     * </desc>
     *
     * @param operateRobotAccountUpdateDTO 修改机器人账号信息数据DTO
     * @return 受影响的行数
     * @author wangzhi
     * @createDate 2018/10/12
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateRobotAccount(OperateRobotAccountUpdateDTO operateRobotAccountUpdateDTO) throws Exception{
        Date nowTime = new Date();
        Date operationTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(operateRobotAccountUpdateDTO.getOperationTimeString());
        Date abateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(operateRobotAccountUpdateDTO.getAbateTimeString());
        AppRobotAccountPO appRobotAccountPO = new AppRobotAccountPO();
        BeanUtils.copyProperties(operateRobotAccountUpdateDTO,appRobotAccountPO);
        if (StringUtils.isNotBlank(operateRobotAccountUpdateDTO.getPassword())) {
            appRobotAccountPO.setPassword(Md5.En(appRobotAccountPO.getPassword()));
        } else {
            appRobotAccountPO.setPassword(null);
        }
        appRobotAccountPO.setOperationTime(operationTime);
        appRobotAccountPO.setAbateTime(abateTime);
        appRobotAccountPO.setUpdateTime(nowTime);
        appRobotAccountPO.setDeveloperUserId(operateRobotAccountUpdateDTO.getUserId());
        Map<String,Object> map = SqlUtil.durableData(appRobotAccountPO,PlatformConstants.TABLE_UPDATE);
        return iOperateRobotAccountDao.updateRobotAccount(map);
    }

    /**
     * <desc>
     *      删除机器人账号信息(批量)
     * </desc>
     *
     * @param ids 数据id，多个用英文逗号连接
     * @return 受影响的行数
     * @author wangzhi
     * @createDate 2018/10/12
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer deleteRobotAccounts(String ids) throws Exception {
        List<String> idList = StringHandlerUtil.splitStringList(ids);
        return iOperateRobotAccountDao.deleteRobotAccounts(idList);
    }

    /**
     * <desc>
     *      获取开发者账号列表
     * </desc>
     *
     * @param operateRobotAccountGetPageDTO 分页查询机器人账号信息数据DTO
     * @return 标准的格式化响应结果
     * @author wangzhi
     * @createDate 2019/09/02
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<Map<String, Object>> getDeveloperList(OperateRobotAccountGetPageDTO operateRobotAccountGetPageDTO) throws Exception {
        return iOperateRobotAccountDao.getDeveloperList(operateRobotAccountGetPageDTO);
    }
}
